package zx.x.modules.yshapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zx.x.common.utils.PageUtils;
import zx.x.common.utils.Query;
import zx.x.modules.yshapp.dao.GoodsAppDao;
import zx.x.modules.yshapp.entity.GoodsEntity;
import zx.x.modules.yshapp.service.GoodsService;

import java.util.*;


@Service("goodsAppService")
public class GoodsAppServiceImpl extends ServiceImpl<GoodsAppDao, GoodsEntity> implements GoodsService {

    @Autowired
    private GoodsAppDao goodsAppDao;
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<GoodsEntity> goodsEntityQueryWrapper = new QueryWrapper<>();
        if (params.get("name")!=null){
            goodsEntityQueryWrapper.eq("delete_flag",0)
                    .eq("on_sale",0)
                    .eq("type", params.get("type"))
                    .eq("community_id", params.get("communityId"))
                    .like("name",params.get("name"))
                    .orderBy(true,false,"update_time");
        }else {
            goodsEntityQueryWrapper.eq("delete_flag",0)
                    .eq("on_sale",0)
                    .eq("type", params.get("type"))
                    .eq("community_id", params.get("communityId"))
                    .orderBy(true,false,"update_time");
        }

        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                goodsEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<GoodsEntity> indexBenefitGoods(Map<String, Object> params) {
       List<GoodsEntity> list = new ArrayList<>();
       int count =  goodsAppDao.selectCount(new QueryWrapper<GoodsEntity>()
               .eq("delete_flag",0)
               .eq("on_sale",0)
               .eq("support_point",0)
               .eq("type",1)
       );
        Random rand = new Random();
       if(count  == 0){//没有
           return null;
       }else if (count  < 3){

       }else{
           while (count < 3) {
//               int index = rand.nextInt(productList.size());
//               Product product = productList.get(index);
//               if (!randomProducts.contains(product)) {
//                   randomProducts.add(product);
//               }
           }
       }


        return Collections.emptyList();
    }
}